﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
public partial class log_LogType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnInsert.Attributes.Add("OnClick", "javascript:return chk();");
            Get_dlTable();
        }
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        /*
        if (txtcode.Text == "")
        {
            msg.Text = "必填";
            return;
        }
        */
        TLogType oLogType = new TLogType();
        oLogType.Id=Guid.NewGuid().ToString();
        oLogType.Name = txtname.Text.Trim();
        oLogType.Authority =2;
        oLogType.SortNum = 0;
        oLogType.InMan = Session["userid"].ToString();
        oLogType.InTime = DateTime.Now;
        if (oLogType.Name == "")
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "msg", "<script>alert('名称不能为空！')</script>");
            txtname.Focus();
            return;
        }
        oLogType.Insert();
        //更新列表
        
        Get_dlTable();
       
    }

    private void Get_dlTable()
    {
        string sSql = "select * from t_log_type where inman='"+Session["userid"].ToString()+"' order by sortnum";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        //关闭
        myReader.Free();
    }
    protected void dlTable_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton b = (LinkButton)e.CommandSource;
        switch (b.CommandName)
        {
            case "Delete":
                TLogType.DeleteByID(b.CommandArgument);
                Get_dlTable();
                hdid.Value = "";
                txtname.Text = "";
                
                dlTable.SelectedIndex = -1;
                break;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dlTable.Items.Count; i++)
        {
            TLogType oLogType = new TLogType(((HiddenField)dlTable.Items[i].FindControl("hfid")).Value);
            oLogType.Select();
            oLogType.Name = ((TextBox)dlTable.Items[i].FindControl("txtname")).Text.Trim();
            oLogType.Authority =int.Parse(((DropDownList )dlTable.Items[i].FindControl("ddlauthoritytype")).SelectedValue);
            
            oLogType.InTime = DateTime.Now;
            if (oLogType.Name == "")
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "msg", "<script>alert('名称不能为空！')</script>");
                ((TextBox)dlTable.Items[i].FindControl("txtname")).Focus();
                return;
            }
            oLogType.Update();

            txtname.Text = "";
            
        }
       
        txtname.Text = "";
        
        dlTable.SelectedIndex = -1;

        Get_dlTable();
    }

    public string GetAuthorityType(string str)
    {
        switch (str)
        {
            //case "1":
            //    return "任何人";
            //case "2":
            //    return "仅好友";
            //case "3":
            //    return "凭密码";
            //default:
            //    return "";
            case "1":
                return "公开";
            case "2":
                return "不公开";
            default:
                return "";
        }
    }

    protected void dlTable_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlauthoritytype");
        if (ddl != null)
        {
            TLogType oLogType = new TLogType(((HiddenField)e.Item.FindControl("hfid")).Value);
            oLogType.Select();
            ddl.SelectedValue = oLogType.Authority.ToString();
        }
    }
}
